﻿@model MvcApplicationTest.Models.RegisterModel

<hgroup class="title">
    <h2>@ViewBag.Title</h2>
</hgroup>

<body onload="Body_OnLoad()">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()

        <fieldset>
            <legend>Registration Form</legend>
            <ol>
                <li>
                    @Html.LabelFor(m => m.UserName)
                    @Html.TextBoxFor(m => m.UserName)
                    @if (ViewBag.IsChangeInfo == null)
                    {
                        <input type="button" id="checkUserBtn" value="Check User Exist" onclick="CheckExist_Onclick()" />
                    }
                </li>
                <li>
                    @Html.LabelFor(m => m.DisplayName)
                    @Html.TextBoxFor(m => m.DisplayName)

                </li>
                @if (ViewBag.IsChangeInfo == true)
                {
                    <li>
                        @Html.LabelFor(m => m.CurrentPassword)
                        @Html.HiddenFor(m => m.HiddenIsChangeInfo)
                        @Html.PasswordFor(m => m.CurrentPassword)
                    </li>
                    <li>
                        @Html.LabelFor(m => m.Password)
                        @Html.HiddenFor(m => m.HiddenPwdForChange)
                        @Html.TextBoxFor(m => m.Password)
                    </li>
                    <li>
                        @Html.LabelFor(m => m.ConfirmPassword)
                        @Html.TextBoxFor(m => m.ConfirmPassword)
                    </li>
                }
                else
                {
                    <li>
                        @Html.LabelFor(m => m.Password)
                        @Html.PasswordFor(m => m.Password)
                    </li>
                    <li>
                        @Html.LabelFor(m => m.ConfirmPassword)
                        @Html.PasswordFor(m => m.ConfirmPassword)
                    </li>
                }
            </ol>
            <input type="submit" value="Register" onclick="Register_OnClick()" />
        </fieldset>
    }

    @if (ViewBag.IsChangeInfo == true)
    {
        <aside>
            <h3>Return:</h3>
            <ul>
                <li>@Html.ActionLink("Order Now", "OrderPage", "Home")</li>
                <li>@Html.ActionLink("Today's Order", "OrderResult", "Home")</li>
                <li>@Html.ActionLink("Order History", "OrderHistory", "Home")</li>
                <li>@Html.ActionLink("Change Your Information", "Register", "Account", new { id = "CYI" }, null)</li>
            </ul>
        </aside>
    }
</body>

<script type="text/javascript">
    function CheckExist_Onclick() {
        var str = $("input[name=UserName]").attr("value");
        if ("" == str) {
            alert("Please input your user name.");
        }
        else {
            $.ajax({
                type: "POST",
                url: "/Home/CheckRegister",
                datatype: "json",
                data: { "str": str },
                async: false,
                success: function (result) {
                    if ("false" == JSON.parse(result).toString()) {
                        alert("u can create the user.");
                    }
                    else {
                        alert("Sorry, the user is exist.");
                    }
                },
                error: function (error) {
                    alert("An error occurred while checking.")
                }
            });
        }
    }

    function Body_OnLoad() {
        if ($("#checkUserBtn").length == 0) {
            $("#UserName").attr("disabled", true);
            $("#UserName").css("background", "yellow");
            $("label[for=Password]").text("New Password");
            $("input[type=submit]").val("Change");
            $("#HiddenIsChangeInfo").val("yes");
            $("#Password").attachEvent("onfocus", "ChangePsw_OnFocus");
            $("#ConfirmPassword").attachEvent("onfocus", "ChangePsw_OnFocus");
        }
    }

    function Register_OnClick() {
        $("#UserName").removeAttr("disabled");
    }

    //没有动态加载上
    function ChangePsw_OnFocus() {
        $("#Password").val("");
        $("#ConfirmPassword").val("");
    }
</script>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
